package avicit.tlk.back.demo.my111.rest;

import avicit.platform6.commons.utils.ComUtil;
import avicit.platform6.commons.utils.StringUtils;
import avicit.platform6.core.excel.ExcelUtil;
import avicit.platform6.core.excel.imp.entity.ExcelImportResult;
import avicit.platform6.core.properties.PlatformConstant;
import avicit.platform6.core.rest.msg.QueryReqBean;
import avicit.platform6.core.rest.msg.QueryRespBean;
import avicit.platform6.core.rest.msg.ResponseMsg;
import avicit.platform6.modules.system.syslog.service.SysLogUtil;
import avicit.tlk.back.demo.my111.dto.My111DTO;
import avicit.tlk.back.demo.my111.dto.My111ImportDTO;
import avicit.tlk.back.demo.my111.service.My111Service;
import io.swagger.annotations.*;
import java.io.IOException;
import java.util.*;
import javax.servlet.http.HttpServletResponse;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

/**
 * My111Rest类
 *
 * @author 朱辉云
 * @version 1.0.0
 * @date 2025-06-13 10:23:55
 * @since 1.0.0
 */
@RestController
@Api(tags = "My111")
@RequestMapping("api/tlk/back/my111")
public class My111Rest {

	private static final Logger logger = LoggerFactory.getLogger(My111Rest.class);

	@Autowired
	private My111Service my111Service;

	/**
	 * 按条件分页查询数据
	 * @param queryReqBean   查询参数
	 * @return 满足条件的分页数据
	 */
	@PostMapping("/find-list-by-page/v1")
	@RequiresPermissions("my111:view")
	@ApiOperation(value = "按条件分页查询数据")
	public ResponseMsg<QueryRespBean<My111DTO>> findListByPage(@ApiParam(value = "查询参数", name = "queryReqBean", required = true) @RequestBody QueryReqBean<My111DTO> queryReqBean) {
		ResponseMsg<QueryRespBean<My111DTO>> responseMsg = new ResponseMsg<>();
		QueryRespBean<My111DTO> queryResults = my111Service.findListByPage(queryReqBean);
		responseMsg.setResponseBody(queryResults);
		SysLogUtil.log("My111", "查询My111信息", PlatformConstant.OpType.select);
		return responseMsg;
	}

	/**
	 * 按条件不分页查询数据
	 * @param queryReqBean   查询参数
	 * @return 满足条件的数据
	 */
	@PostMapping("/find-list/v1")
	@RequiresPermissions("my111:view")
	@ApiOperation(value = "按条件不分页查询数据")
	public ResponseMsg<List<My111DTO>> findList(@ApiParam(value = "查询参数", name = "queryReqBean", required = true) @RequestBody QueryReqBean<My111DTO> queryReqBean) {
		ResponseMsg<List<My111DTO>> responseMsg = new ResponseMsg<>();
		List<My111DTO> queryResults = my111Service.findList(queryReqBean);
		responseMsg.setResponseBody(queryResults);
		SysLogUtil.log("My111", "查询My111信息", PlatformConstant.OpType.select);
		return responseMsg;
	}

	/**
	 * 根据主键查询数据
	 * @param id   主键
	 * @return 主键查询的数据
	 */
	@GetMapping("/get/{id}/v1")
	@RequiresPermissions("my111:view")
	@ApiOperation(value = "根据主键查询数据")
	public ResponseMsg<My111DTO> getById(@ApiParam(value = "主键", name = "id", required = true) @PathVariable("id") String id) {
		ResponseMsg<My111DTO> responseMsg = new ResponseMsg<>();
		My111DTO my111 = my111Service.get(id);
		responseMsg.setResponseBody(my111);
		SysLogUtil.log4Query(my111);
		return responseMsg;
	}

	/**
	 * 保存表单对象
	 * @param entity   表单数据
	 * @return 主键
	 */
	@PostMapping("/save/v1")
	@RequiresPermissions(value = { "my111:add", "my111:edit", "my111:save" }, logical = Logical.OR)
	@ApiOperation(value = "保存表单对象")
	public ResponseMsg<String> save(@ApiParam(value = "表单数据", name = "entity", required = true) @RequestBody My111DTO entity) {
		ResponseMsg<String> responseMsg = new ResponseMsg<>();
		if (my111Service.get(entity.getId()) != null) {
			responseMsg.setResponseBody(String.valueOf(my111Service.updateAll(entity)));
		} else {
			responseMsg.setResponseBody(my111Service.insert(entity));
		}
		return responseMsg;
	}

	/**
	 * 修改部分表单字段
	 * @param entity   表单数据
	 * @return 受影响的行数
	 */
	@PutMapping("/update-sensitive/v1")
	@RequiresPermissions(value = { "my111:edit", "my111:save" }, logical = Logical.OR)
	@ApiOperation(value = "修改部分表单字段")
	public ResponseMsg<Integer> updateSensitive(@ApiParam(value = "表单数据", name = "entity", required = true) @RequestBody My111DTO entity) {
		ResponseMsg<Integer> responseMsg = new ResponseMsg<>();
		responseMsg.setResponseBody(my111Service.updateSensitive(entity));
		return responseMsg;
	}

	/**
	 * 修改全部表单字段
	 * @param entity   表单数据
	 * @return 受影响的行数
	 */
	@PutMapping("/update-all/v1")
	@RequiresPermissions(value = { "my111:edit", "my111:save" }, logical = Logical.OR)
	@ApiOperation(value = "修改全部表单字段")
	public ResponseMsg<Integer> updateAll(@ApiParam(value = "表单数据", name = "entity", required = true) @RequestBody My111DTO entity) {
		ResponseMsg<Integer> responseMsg = new ResponseMsg<>();
		responseMsg.setResponseBody(my111Service.updateAll(entity));
		return responseMsg;
	}

	/**
	 * 按主键单条删除数据
	 * @param id   主键
	 * @return 受影响的行数
	 */
	@DeleteMapping("/delete-by-id/{id}/v1")
	@RequiresPermissions("my111:delete")
	@ApiOperation(value = "按主键单条删除数据")
	public ResponseMsg<Integer> deleteById(@ApiParam(value = "主键", name = "id", required = true) @PathVariable("id") String id) {
		ResponseMsg<Integer> responseMsg = new ResponseMsg<>();
		responseMsg.setResponseBody(my111Service.delete(id));
		return responseMsg;
	}

	/**
	 * 主表数据批量删除数据
	 * @param ids   主键
	 * @return 受影响的行数
	 */
	@DeleteMapping("/delete-by-ids/v1")
	@RequiresPermissions("my111:delete")
	@ApiOperation(value = "主表数据批量删除数据")
	public ResponseMsg<Integer> deleteByIds(@ApiParam(value = "主键", name = "ids", required = true) @RequestBody String[] ids) {
		ResponseMsg<Integer> responseMsg = new ResponseMsg<>();
		responseMsg.setResponseBody(my111Service.deleteByIds(ids));
		return responseMsg;
	}

	/**
	 * 导入模板下载
	 * @param response   响应
	 * @return
	 */
	@GetMapping("/download-template/v1")
	@RequiresPermissions("my111:import")
	@ApiOperation(value = "导入模板下载")
	public void downloadTemplate(@ApiParam(value = "响应", name = "response", required = true) HttpServletResponse response) throws IOException {
		ExcelUtil.downloadTemplate(response, My111ImportDTO.class, "sheet1");
	}

	/**
	 * 导入数据
	 * @param impExcelFile   导入文件
	 * @param formData   表单数据
	 * @return Object
	 */
	@PostMapping("/import-data/v1")
	@RequiresPermissions("my111:import")
	@ApiOperation(value = "导入数据")
	public ResponseMsg<Object> importDataV2(
		@ApiParam(value = "导入文件", name = "impExcelFile", required = true) @RequestPart("impExcelFile") MultipartFile impExcelFile,
		@ApiParam(value = "表单数据", name = "formData", required = true) @RequestParam("formData") String formData
	) throws IOException {
		ResponseMsg<Object> responseMsg = new ResponseMsg<>();
		List<Map<String, String>> excelList = ExcelUtil.importExcel(impExcelFile.getInputStream(), My111ImportDTO.class);
		ExcelImportResult excelImportResult = my111Service.importData(excelList, formData);
		responseMsg.setResponseBody(excelImportResult);
		return responseMsg;
	}

	/**
	 * 按条件同步导出数据
	 * @param my111   查询参数
	 * @param response   响应
	 * @return
	 */
	@PostMapping("/export-data/v1")
	@RequiresPermissions("my111:export")
	@ApiOperation(value = "按条件同步导出数据")
	public void exportData(
		@ApiParam(value = "查询参数", name = "my111", required = true) @RequestBody QueryReqBean<My111DTO> my111,
		@ApiParam(value = "响应", name = "response", required = true) HttpServletResponse response
	) throws IOException {
		List<My111DTO> queryResults = my111Service.findList(my111);
		ExcelUtil.exportExcel(response, queryResults, My111DTO.class, "导出数据", "sheet1");
		SysLogUtil.log(
			new My111DTO().getLogTitle(),
			new My111DTO().getLogTitle() + "共导出" + queryResults.size() + "条数据",
			PlatformConstant.OpType.fileExport,
			PlatformConstant.OpResult.success,
			ComUtil.getId(),
			"My111"
		);
	}
}
